package showtime.framework.display
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import showtime.framework.utils.GraphicUtility;

	public class HitArea extends Sprite
	{
		/**
		 *  Bitmap Data.
		 */
		private var bitmap:Bitmap = null;
		
		/**
		 *  Effects.
		 */
		private var effects:Array = null;
		
		/**
		 * Callback function. 
		 */		
		private var callback:Function = null;
		/**
		 * Data. 
		 */		
		public var data:Object = null;
		
		//--------------------------------------------------------------------------
		//
		//  Initializing.
		//
		//--------------------------------------------------------------------------
		
		public function HitArea(source:*, data:Object = null, callback:Function = null)
		{
			this.callback = callback;
			this.data = data;
			
			if (source is DisplayObject)
			{
				var bitmapData:BitmapData = GraphicUtility.copyAsBitmapData(source);
				
				bitmap = new Bitmap(bitmapData);
			
			}			
			else if (source is BitmapData)
			{
				bitmap = new Bitmap(source);
			}
			else if (source is Bitmap)
			{
				bitmap = source.bitmapData;
			}
			
			addChild(bitmap);
			
			addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			addEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  公开API
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 添加滤镜效果数组
		 * @param        effectArr
		 */
		public function addEffect(effectArr:Array):void
		{
			effects = effectArr;
		}
		
		/**
		 * 摧毁
		 */
		public function destroy():void 
		{
			removeEventListener(MouseEvent.CLICK, clickHandler);
			removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			removeEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			removeEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
			
			effects = null;
			
			removeChild(bitmap);
			bitmap.bitmapData.dispose();
		}
		
		//--------------------------------------------------------------------------
		//
		//  事件处理函数
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 鼠标滑入
		 * @param        evt
		 */
		private function rollOverHandler(evt:MouseEvent):void 
		{        
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			
			addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
		}
		
		/**
		 * 鼠标滑出
		 * @param        evt
		 */
		private function rollOutHandler(evt:MouseEvent):void 
		{        
			removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			
			filters = null;
		}
		
		private function clickHandler(evt:MouseEvent):void
		{
			if (bitmap.bitmapData.getPixel32(mouseX, mouseY) >>> 24 != 0x00)
			{
				callback(data);
			}
		}
		/**
		 * 处理鼠标滑入操作，忽略透明区域
		 * @param        evt
		 */
		private function enterFrameHandler(evt:Event):void 
		{
			if (bitmap.bitmapData.getPixel32(mouseX, mouseY) >>> 24 != 0x00)
			{
				if(effects != null && effects.length > 0)
					filters = effects;
			}
			else
			{
				filters = null;
			}
		}
		
	}
	
}